package com.healthcloud.controller;

import com.healthcloud.annotation.SysLog;
import com.healthcloud.constants.PagineConsts;
import com.healthcloud.entity.SysMenuEntity;
import com.healthcloud.entity.SysRoleEntity;
import com.healthcloud.service.SysMenuService;
import com.healthcloud.service.SysRoleMenuService;
import com.healthcloud.service.SysRoleService;
import com.healthcloud.utils.Constant;
import com.healthcloud.utils.ApiRes;
import com.healthcloud.utils.PageParam;
import com.healthcloud.validator.ValidatorUtils;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 角色管理
 * Created by yu on 2017/6/6.
 */
@ApiIgnore
@Controller
@RequestMapping("/sys/role")
public class SysRoleController extends AbstractController {

    @Autowired
    private SysRoleService sysRoleService;
    @Autowired
    private SysRoleMenuService sysRoleMenuService;

    @Autowired
    private SysMenuService sysMenuService;


    /**
     * 角色列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("sys:role:list")
    public String list() {

        return "sys/role/roleMgr";

    }


    /**
     * 所有角色列表
     */
    @RequestMapping("/search")
    @ResponseBody
    public Map<String, Object> seartList(@ModelAttribute("pageParam") PageParam pageParam, SysRoleEntity entity) {


        //查询列表数据
        if (!getUser().getUsername().equals(Constant.ADMIN)) {
            //超级管理员
            entity.setCreateUserId(getUserId());
        }

        List<SysRoleEntity> configList = sysRoleService.queryList(entity, pageParam);
        int total = sysRoleService.queryTotal(entity);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("dataList", configList);
        map.put("iTotalRecords", total);
        map.put("iTotalDisplayRecords", total);
        map.put("sEcho", pageParam.getsEcho());

        return map;
    }


    /**
     * 所有角色列表
     */
    @RequestMapping("/tosave")
    @RequiresPermissions("sys:role:save")
    public String toSaveRole(Model model) {

        PageParam pageParam = new PageParam();
        pageParam.setiDisplayStart(PagineConsts.iDisplayStart);
        pageParam.setiDisplayLength(PagineConsts.iDisplayLength);

        //查询列表数据
        List<SysMenuEntity> menuList = null;

        //只有超级管理员，才能查看所有管理员列表
        if (getUser().getUsername().equals(Constant.ADMIN)) {
            menuList = sysMenuService.queryList(new SysMenuEntity(), pageParam);
        } else {
            menuList = sysMenuService.queryUserList(getUserId());
        }

        model.addAttribute("menuList", menuList);

        return "sys/role/addOrUpdateRole";
    }

    /**
     * 保存角色
     */
    @SysLog("保存角色")
    @RequestMapping("/save")
    @ResponseBody
    public ApiRes save(SysRoleEntity role) {

        ValidatorUtils.validateEntity(role);

        role.setCreateUserId(getUserId());
        sysRoleService.save(role);

        return ApiRes.SUCCESS;
    }


    @RequestMapping("/toupdate")
    @RequiresPermissions("sys:role:update")
    public String toUpdateRole(@Param("roleId") String roleId,Model model) {

        PageParam pageParam = new PageParam();
        pageParam.setiDisplayStart(PagineConsts.iDisplayStart);
        pageParam.setiDisplayLength(PagineConsts.iDisplayLength);

        SysRoleEntity role = sysRoleService.queryObject(Long.valueOf(roleId));

        //查询角色对应的菜单
        List<Long> menuIdList = sysRoleMenuService.queryMenuIdList(Long.valueOf(roleId));
        role.setMenuIdList(menuIdList);

        model.addAttribute("entity",role);

        //
        //查询列表数据
        List<SysMenuEntity> menuList = null;

        //只有超级管理员，才能查看所有管理员列表
        if (getUser().getUsername().equals(Constant.ADMIN)) {
            menuList = sysMenuService.queryList(new SysMenuEntity(), pageParam);
        } else {
            menuList = sysMenuService.queryUserList(getUserId());
        }

        for(Long menuId : menuIdList){
            for(SysMenuEntity menuEntity : menuList){

                menuEntity.setOpen(true);

                if(menuId.equals(menuEntity.getMenuId())){
                    menuEntity.setChecked(true);
                }
            }

        }

        model.addAttribute("menuList",menuList);
        return "sys/role/addOrUpdateRole";

    }

    /**
     * 修改角色
     */
    @SysLog("修改角色")
    @RequestMapping("/update")
    @ResponseBody
    public ApiRes update(SysRoleEntity role) {

        ValidatorUtils.validateEntity(role);
        role.setCreateUserId(getUserId());
        sysRoleService.update(role);

        return ApiRes.SUCCESS;
    }

    /**
     * 删除角色
     */
    @SysLog("删除角色")
    @RequestMapping("/delete")
    @RequiresPermissions("sys:role:delete")
    @ResponseBody
    public ApiRes delete(Long[] roleIds) {

        sysRoleService.deleteBatch(roleIds);

        return ApiRes.SUCCESS;
    }
}
